<?php
/**
 * This file is part of workerman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link http://www.workerman.net/
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */

/**
 * 用于检测业务代码死循环或者长时间阻塞等问题
 * 如果发现业务卡死，可以将下面declare打开（去掉//注释），并执行php start.php reload
 * 然后观察一段时间workerman.log看是否有process_timeout异常
 */
//declare(ticks=1);

use \GatewayWorker\Lib\Gateway;
use \Monolog\Logger;
use \Monolog\Handler\StreamHandler;
use \Workerman\MySQL\Connection;
use \Workerman\Worker;

/**
 * 主逻辑
 * 主要是处理 onConnect onMessage onClose 三个方法
 * onConnect 和 onClose 如果不需要可以不用实现并删除
 */
class Events
{
    public static function onWorkerStart()
    {
        // 将db实例存储在全局变量中(也可以存储在某类的静态成员中)
        global $db;
        // $db = new Workerman\MySQL\Connection('127.0.0.1', '8889', 'root', 'root', 'test_kqm_itqiyao_com');
    }
    /**
     * 当客户端连接时触发
     * 如果业务不需此回调可以删除onConnect
     * 
     * @param int $client_id 连接id
     */
    public static function onConnect($client_id)
    {
        self::logs('链接',"链接-".$client_id.":");
        // // 向当前client_id发送数据
        // Gateway::sendToClient($client_id, "Hello $client_id\r\n");
        // // 向所有人发送
        // Gateway::sendToAll("$client_id login\r\n");
    }
    
   /**
    * 当客户端发来消息时触发
    * @param int $client_id 连接id
    * @param mixed $message 具体消息
    */
   public static function onMessage($client_id, $message)
   {
        // $hex_info = self::hexToStr("bebc1ff");
        // echo "hex:".$hex_info;
        // 向所有人发送 
        $update_data['user_name'] = 'socket_add111';
        $message_info = base64_encode($message);
        $message_json = json_encode($message);
        // self::delete("jm_test","id=1");
        self::logs($message_info,"客户端发送的消息-".$client_id.":");
        // self::logs($message_json,"客户端发送的消息-two-".$client_id.":");
        echo $client_id."发送的数据-:".$_SERVER['GATEWAY_PORT']."-".$message."<br/>";
        Gateway::sendToAll("$client_id said $message\r\n");
   }
   
   /**
    * 当用户断开连接时触发
    * @param int $client_id 连接id
    */
   public static function onClose($client_id)
   {
       // 向所有人发送 
       echo $client_id."断开了链接<br/>";
       self::logs($client_id,"断开了链接-".$client_id.":");
       GateWay::sendToAll("$client_id logout\r\n");
   }

   /**
    * 获取所有数据
    */
    public static function query($table, $field, $where)
    {
        global $db;
        $info = $db->select($field)->from($table)->where($where)->query();
        return $info;
    }

    /**
     * 获取一行数据
     */
    public static function row($table, $field, $where)
    {
        global $db;
        $info = $db->select($field)->from($table)->where($where)->row();
        return $info;
    }

    /**
     * 获取一列数据
     */
    public static function column($table, $field, $where)
    {
        global $db;
        $info = $db->select($field)->from($table)->where($where)->column();
        return $info;
    }

    /**
     * 获取单个值
     */
    public static function value($table, $field, $where)
    {
        global $db;
        $info = $db->select($field)->from($table)->where($where)->single();
        return $info;
    }

    /**
     *  插入
     */
    public static function insert($table, $insert)
    {
        global $db;
        $insert = $db->insert($table)->cols($insert)->query();
        return $insert;
    }

    /**
     * 更新
     * $db->update('Persons')->cols(array('sex'=>'F'))->where('ID=1')->query();
     */
    public static function update($table, $update, $where)
    {
        global $db;
        $update = $db->update($table)->cols($update)->where($where)->query();
        return $update;
    }
    /**
     * 删除
     */
    public static function delete($table, $where)
    {
        global $db;
        $delete = $db->delete($table)->where($where)->query();
        return $delete;
    }

   /**
    * 日志
    */
    public static function logs($data, $msg='DATA', $extra='')
    {
        if (!is_array($data)) {
            $content['data'] = $data;
        } else {
            $content = $data;
        }
        $content['server'] = $_SERVER;
        $log = new Logger('name');
        $log->pushHandler(new StreamHandler('path/to/'.date('Ymd').'.log', Logger::INFO));
        $log->info($msg,$content);
    }
}
